package com.runvp.netty.impl;

import com.runvp.netty.decoder.ConversionDecoder;
import com.runvp.netty.server.NettyServerHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * *
 * NettyChannelInitializerImpl 业务Handler
 *
 * @author xkk
 * @Date 2020/07/13 07:13
 **/
@Component
@Slf4j
public class NettyChannelInitializerImpl extends ChannelInitializer<SocketChannel> {
    @Autowired
    private NettyServerHandler nettyServer1Handler;


    @Override
    protected void initChannel(SocketChannel ch) {
        // 设置接收缓冲区大小
        ch.config().setReceiveBufferSize(1024 * 1024); // 例如设置为1MB
        int localPort = ch.localAddress().getPort();
        log.debug("Netty连接的端口号: " + localPort);
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new ConversionDecoder());
        //添加自己业务Handler
        pipeline.addLast(nettyServer1Handler);
    }
}
